package com.ruoyi.goods.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.cangku.mapper.KucunpandianMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.goods.mapper.ShangpinxinxiMapper;
import com.ruoyi.goods.domain.Shangpinxinxi;
import com.ruoyi.goods.service.IShangpinxinxiService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-18
 */
@Service
public class ShangpinxinxiServiceImpl implements IShangpinxinxiService
{
    @Autowired
    private ShangpinxinxiMapper shangpinxinxiMapper;

    @Autowired
    private KucunpandianMapper kucunpandianMapper;

    /**
     * 查询商品信息
     *
     * @param id 商品信息主键
     * @return 商品信息
     */
    @Override
    public Shangpinxinxi selectShangpinxinxiById(Long id)
    {
        return shangpinxinxiMapper.selectShangpinxinxiById(id);
    }

    @Override
    public Shangpinxinxi selectShangpinxinxiByShangpinId(String shangpinId){return shangpinxinxiMapper.selectShangpinxinxiByShangpinId(shangpinId);}

    /**
     * 查询商品信息列表
     *
     * @param shangpinxinxi 商品信息
     * @return 商品信息
     */
    @Override
    public List<Shangpinxinxi> selectShangpinxinxiList(Shangpinxinxi shangpinxinxi)
    {
        return shangpinxinxiMapper.selectShangpinxinxiList(shangpinxinxi);
    }

    /**
     * 新增商品信息
     *
     * @param shangpinxinxi 商品信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertShangpinxinxi(Shangpinxinxi shangpinxinxi)
    {
        Shangpinxinxi sp  = shangpinxinxiMapper.selectShangpinxinxiByShangpinId(shangpinxinxi.getShangpinId());
        if(sp != null){
            throw new RuntimeException("商品条码"+shangpinxinxi.getShangpinId()+"已存在！");
        }
        return shangpinxinxiMapper.insertShangpinxinxi(shangpinxinxi);
    }

    /**
     * 修改商品信息
     *
     * @param shangpinxinxi 商品信息
     * @return 结果
     */
    @Override
    public int updateShangpinxinxi(Shangpinxinxi shangpinxinxi)
    {
        return shangpinxinxiMapper.updateShangpinxinxi(shangpinxinxi);
    }

    /**
     * 批量删除商品信息
     *
     * @param ids 需要删除的商品信息主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteShangpinxinxiByIds(Long[] ids)
    {
        for(Long id:ids) {
            BigDecimal count = kucunpandianMapper.selectCountKucunpandianByGoodId(id);
            if (count.compareTo(BigDecimal.ZERO) > 0) throw new RuntimeException("商品当前库存不为0,不能删除");
        }
        return shangpinxinxiMapper.deleteShangpinxinxiByIds(ids);
    }

    /**
     * 删除商品信息信息
     *
     * @param id 商品信息主键
     * @return 结果
     */
    @Override
    public int deleteShangpinxinxiById(Long id)
    {
        BigDecimal count = kucunpandianMapper.selectCountKucunpandianByGoodId(id);
        if(count.compareTo(BigDecimal.ZERO) > 0) throw new RuntimeException("商品当前库存不为0,不能删除");
        return shangpinxinxiMapper.deleteShangpinxinxiById(id);
    }
}
